package com.tinywind.Sword.Controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tinywind.Sword.Entity.TokenEntity;
import com.tinywind.Sword.Entity.UserEntity;
import com.tinywind.Sword.Service.TodoService;
import com.tinywind.Sword.Service.UserService;
import com.tinywind.Sword.Utility.GaJJwtManager;
import com.tinywind.Sword.Utility.GaJTimeManager;
import com.tinywind.Sword.Utility.ResponseCodeManager;
import com.tinywind.Sword.Dto.Data;
import com.tinywind.Sword.Dto.Page;
import com.tinywind.Sword.Dto.Result;

@RestController
@CrossOrigin
@RequestMapping("/User")
public class UserController extends BaseController {
		private Logger logger = LoggerFactory.getLogger(this.getClass());
	
		@Autowired
		private UserService mService;
		
		@RequestMapping(value = "/Login", method = RequestMethod.POST)
		@ResponseBody
		public Result<UserEntity> login(@NonNull String account,String password) {
			logger.info("UserController/login,account:%s,password:%s",account,password);
			try {
				UserEntity entity = mService.login(account, password);
				//用户不存在. -1;
				if(entity == null) {
					Result<UserEntity> result = new Result<UserEntity>(-1,"No such account");
					return result;
				}
				if(entity.password.equals(password)) {
					//登录成功
					Result<UserEntity> result = new Result<UserEntity>(entity);
					String token = storeToken(entity.account, entity.id);
					response.setHeader(GaJJwtManager.ms_authKey,token);
					return result;
				}
				else {
					Result<UserEntity> result = new Result<UserEntity>(-2, "Invalid password");
					return result;
				}
			}
			catch (Exception e) {
				logger.debug("TodoController/search exception,%s",e.getMessage());
				e.printStackTrace();
				return null;
			}
			
		}
		
		public String storeToken(String userAccount,Integer userId) {
			String token = GaJJwtManager.generatorToken(userAccount, userId);
			String info = String .format("UserController/storeToken,token is: %s",token);
			logger.info(info);
			String expireTime = GaJTimeManager.sharedInstance.stdTimeStringAfterNow(GaJJwtManager.ms_expireTime);
			TokenEntity entity = new TokenEntity(token,userId,expireTime);
			try {
				mService.insertToken(entity);
			}
			catch (Exception e) {
				//重复插入.
				info = e.getLocalizedMessage();
				logger.warn(info);
			}
			return token;
		}
		
		@RequestMapping(value = "/NoToken", method = RequestMethod.GET)
		@ResponseBody
		public Result<Object> noToken(int code) {
			String info = String .format("UserController/noToken,code is: %d",code);
			logger.info(info);
			String message = ResponseCodeManager.getCodeMessage(code);
			Result<Object> result = new Result<Object>(code,message);
			return result;
		}
	}